package com.indexingsystem.boss.common;

import java.io.IOException;
import java.nio.charset.Charset;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.util.EntityUtils;
import org.springframework.web.multipart.MultipartFile;

public class ImportClientHttpUtil {
	/**
	 * 
	 * @param 要请求的接口地址
	 * @param post参数
	 * @return 接口返回的数据
	 * @throws IOException
	 */
	public static Object HttpPost(String url, String parameters) throws IOException {
		HttpClient httpclient = new DefaultHttpClient();
		httpclient.getParams().setParameter( HttpConnectionParams.CONNECTION_TIMEOUT, 30000 );
		httpclient.getParams().setParameter( HttpConnectionParams.SO_TIMEOUT, 30000 );
		try {
			HttpPost httppost = new HttpPost( url );
			httppost.setEntity( new StringEntity( parameters, Charset.forName( "UTF-8" ) ) );
			httppost.addHeader( "Content-type", "application/json; charset=utf-8" );
			httppost.setHeader( "Accept", "application/json" );
			// 处理请求，得到响应
			HttpResponse response = httpclient.execute( httppost );
			// 打印返回的结果
			HttpEntity entity = response.getEntity();
			String result = EntityUtils.toString( entity, "UTF-8" );
			return result;
		} finally {
			httpclient.getConnectionManager().shutdown();
		}
	}

	public static String HttpPostJson(String url, String parameters) {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost( url );
		RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout( 180 * 1000 ).setConnectionRequestTimeout( 180 * 1000 )
				.setSocketTimeout( 180 * 1000 ).setRedirectsEnabled( true ).build();

		httpPost.setConfig( requestConfig );
		// httpPost.setHeader("Content-Type", "application/json"); //
		try {
			// httpPost.setEntity(new StringEntity(parameters,
			// ContentType.create("application/json",
			// "utf-8")));
			httpPost.setEntity( new StringEntity( parameters, Charset.forName( "UTF-8" ) ) );
			httpPost.addHeader( "Content-type", "application/json; charset=utf-8" );
			httpPost.setHeader( "Accept", "application/json" );
			// System.out.println( "request parameters" + EntityUtils.toString(
			// httpPost.getEntity() ) );
			HttpResponse response = httpClient.execute( httpPost );
			// System.out.println( " code:" +
			// response.getStatusLine().getStatusCode() );
			// System.out.println( "doPostForInfobipUnsub response" +
			// response.getStatusLine().toString() );
			// return String.valueOf(response.getStatusLine().getStatusCode());
			// 打印返回的结果
			HttpEntity entity = response.getEntity();
			String result = EntityUtils.toString( entity, "UTF-8" );
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return "post failure :caused by-->" + e.getMessage().toString();
		} finally {
			if (null != httpClient) {
				try {
					httpClient.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

	public static String HttpPostStoreJson(String url, String parameters) {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost( url );
		RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout( 180 * 1000 ).setConnectionRequestTimeout( 180 * 1000 )
				.setSocketTimeout( 180 * 1000 ).setRedirectsEnabled( true ).build();

		httpPost.setConfig( requestConfig );
		// httpPost.setHeader("Content-Type", "application/json"); //
		try {
			// httpPost.setEntity(new StringEntity(parameters,
			// ContentType.create("application/json",
			// "utf-8")));
			httpPost.setEntity( new StringEntity( parameters, Charset.forName( "UTF-8" ) ) );
			httpPost.addHeader( "Content-type", "application/json; charset=utf-8" );
			httpPost.setHeader( "Accept", "application/json" );
//			System.out.println( "request parameters" + EntityUtils.toString( httpPost.getEntity() ) );
			HttpResponse response = httpClient.execute( httpPost );
//			System.out.println( " code:" + response.getStatusLine().getStatusCode() );
//			System.out.println( "doPostForInfobipUnsub response" + response.getStatusLine().toString() );
			// return String.valueOf(response.getStatusLine().getStatusCode());
			// 打印返回的结果
			HttpEntity entity = response.getEntity();
			String result = EntityUtils.toString( entity, "UTF-8" );
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return "post failure :caused by-->" + e.getMessage().toString();
		} finally {
			if (null != httpClient) {
				try {
					httpClient.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

	/**
	 * imageType=1为需要生成缩略图,为空就不需要.
	 * 
	 * @param url
	 * @param filename
	 * @param targert
	 * @param multipartFile
	 * @param imageType
	 * @return
	 * @throws IOException
	 */
	public static String HttpImagePost(String url, String filename, String targert, MultipartFile multipartFile, String imageType)
			throws IOException, Exception {
		// 判断fileName 扩展名
		String prefix = filename.substring( filename.lastIndexOf( "." ) + 1 );
		prefix = prefix.toLowerCase();
		if (prefix.equals( "js" ) || prefix.equals( "ts" ) || prefix.equals( "sh" ) || prefix.equals( "exe" ) || prefix.equals( "as" )
				|| prefix.equals( "python" ) || prefix.equals( "ruby" ) || prefix.equals( "bat" ) || prefix.equals( "html" ) || prefix.equals( "css" )
				|| prefix.equals( "jsp" ) || prefix.equals( "asp" )) {
			throw new Exception( "上传文件后缀不合法,请重新上传合法的文件！" );
		}

		HttpClient client = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost( url );// 通过post传递
		MultipartEntityBuilder muit = MultipartEntityBuilder.create();
		// 如果imageType= 1
		if (StringUtils.isNotBlank( imageType )) {
			muit.addTextBody( "target", targert + "|" + "min" );
		} else {
			muit.addTextBody( "target", targert );
		}
		// muit.addTextBody("imageType", imageType);
		// muit.addTextBody("target", targert);
		String result = null;
		try {
			muit.addBinaryBody( "file", multipartFile.getInputStream(), ContentType.MULTIPART_FORM_DATA, filename );
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		HttpEntity entity = muit.build();
		httpPost.setEntity( entity );
		/** 发送请求 */
		try {
			HttpResponse response = client.execute( httpPost );
			// 判断是否上传成功 返回200
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				result = EntityUtils.toString( response.getEntity() );
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			client.getConnectionManager().shutdown();
		}
		return result;
	}
}